﻿#include "infoteacher.h"

teacherInfo::teacherInfo()
{
    this->setM=new modelSet;
    this->op=new fileOperator;
}

void teacherInfo::setInfo(QString name, QString id, QString password, QString college, QString level, QStringList course)
{
    this->personInfo::setInfo(name,id);
    this->teaCollege=college;
    this->teaLevel=level;
    this->teaPassword=password;
    this->teaCourse=course;

}

void teacherInfo::loadInfo()
{
    //加载全校课程信息
    QStringList _line1;
    this->op->readFromInfoFile("courseInfo.csv",_line1);
    for(int i=0;i<_line1.length();i++)
    {
        QString line=_line1.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        courseInfo a;
        a.setCourseInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(7),linesplit.at(8),linesplit.at(9),linesplit.at(10),linesplit.at(11),linesplit.at(12));
        allCourse.push_back(a);
    }
    //加载个人课程信息到类中
    for(int i=0;i<teaCourse.length();i++)
    {
        myCourse.push_back(matchCourseWithId(teaCourse.at(i),allCourse));
    }
    //加载所有学生
    QStringList _line2;
    this->op->readFromInfoFile("stuInfo.csv",_line2);
    for(int i=0;i<_line2.length();i++)
    {
        QString line=_line2.at(i);
        QStringList selectedCourse;
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        for(int i=7;i<linesplit.length();i++)
        {
            selectedCourse.append(linesplit.at(i));
        }
        studentInfo a;
        a.setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(2),selectedCourse);
        allStudent.push_back(a);
    }
}

bool teacherInfo::loadMyStudent(QString selectId)
{
    myStudent.clear();
    for(int i=0;i<allStudent.length();i++)
    {
        for(int j=0;j<allStudent.at(i).getSelectedCourse().length();j++)
        {
            if(selectId==allStudent.at(i).getSelectedCourse().at(j))
            {
                myStudent.push_back(allStudent.at(i));
            }
        }
    }
    if(myStudent.size()==0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool teacherInfo::modifyPassword(QString oldPassword, QString newPassword)
{
    if(oldPassword==this->teaPassword)
    {
        if(newPassword.length()<6)
        {
            return false;
        }
        else
        {
            this->teaPassword=newPassword;
            return  true;
        }
    }
    else
    {
        return false;
    }
}

void teacherInfo::checkMyInfo(QStandardItemModel *model, int curRow) const
{
    QStringList line;
    line.append(this->getName());
    line.append(this->getId());
    line.append(this->teaCollege);
    line.append(this->teaLevel);
    line.append(this->teaCourse);
    this->setM->setTeacherTitle(model);
    this->setM->setTeacherItem(model,line,curRow);
}

void teacherInfo::checkMyStudent(QStandardItemModel *model)
{
    for(int i=0;i<myStudent.length();i++)
    {
        myStudent.at(i).checkMyInfo(model,i);
    }
}

void teacherInfo::checkMyCourse(QStandardItemModel *model)
{
    this->setM->setCourseTitle(model);
    for(int i=0;i<myCourse.length();i++)
    {
        myCourse.at(i).displayThisCourse(model,i);
    }
}

QStringList teacherInfo::getMyCourse()
{
    return this->teaCourse;
}

QStringList teacherInfo::getAllInfo() const
{
    QStringList allInfo;
    allInfo.append(getName());
    allInfo.append(getId());
    allInfo.append(teaPassword);
    allInfo.append(teaCollege);
    allInfo.append(teaLevel);
    allInfo.append(teaCourse);
    return allInfo;
}

QString teacherInfo::getCollege()
{
    return teaCollege;
}

QString teacherInfo::getLevel()
{
    return teaLevel;
}

void teacherInfo::saveAllInfo()
{
    this->op->modifyFile("teacherInfo",getAllInfo());
    for(int i =0;i<waitToLoadCourse.length();i++)
    {
        this->op->appendToFile("waitToLoadCourse",waitToLoadCourse.at(i).getAllInfo());
    }
}

void teacherInfo::addCourse(QString id)
{
    teaCourse.append(id);
}

void teacherInfo::removeCourse(QString id)
{
    for(int i=0;i<teaCourse.length();i++)
    {
        if(teaCourse.at(i)==id)
        {
            teaCourse.removeAt(i);
        }
    }
}

void teacherInfo::submitCourse(courseInfo &course)
{
    waitToLoadCourse.push_back(course);
}

courseInfo &teacherInfo::matchCourseWithId(const QString courseSelectId, QVector<courseInfo> &course)
{
    for(int i=0;i<course.length();i++)
    {
        if(course.at(i).getCourseSelectId()==courseSelectId)
        {
            return course[i];
        }
    }
}



